package com.itheima.controller;


import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")  //抽取公共的请求路径，最终的资源路径=类上面的value加方法上面注解的value
public class DeptController {

    @Autowired
    private DeptService deptService;

    /*
    * 该接口用于部门列表数据查询
    * */

    @GetMapping
    public Result select(){
        log.info("查询部门列表");
        List<Dept> depts = deptService.select();
        return Result.success(depts);
    }

    /*
    * 该接口用于根据ID删除部门数据
    * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) throws Exception{
        log.info("删除部门："+id);
        deptService.delete(id);
        return Result.success();
    }

    /*
    * 该接口用于插入部门数据
    * */
    @PostMapping
    public Result insert(@RequestBody Dept dept){
        log.info("插入数据");
        deptService.insert(dept);
        return Result.success();
    }

    /*
    * 该接口用于根据ID查询部门数据
    * */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    /*
    * 该接口用于修改部门数据
    * */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        deptService.update(dept.getId(),dept.getName());
        System.out.println("dept = " + dept);
        return Result.success();
    }
}
